﻿using System;
using System.Text;
using mfRCF.Core;
using mfRCF.Core.Extensions;
using mfRCF.Rss;
using Microsoft.SPOT;
using mfXmlSerializer;

namespace mfRCF.NetduinoPlus2.TestApp
{
    public class RssGetCommand : mfRcfCommand
    {
        public RssGetCommand(string httpPath)
            : base(httpPath, new mfGETMethod()) { }


        public override void ExecuteCommand(mfIHttpContext httpContext)
        {
            rss rssSample = RSSSampleDataFactory.CreateRSSSample_Complete();
            var rssSerializer = new mfXmlTypedSerializer(rssSample.GetType());

            //hang in rssVersionWriterAction in serialzation-process
            rssSerializer.ElementSerialized += mfWriteRSSVersionAction.WriteRSSVersionAction;

            //serialze xml
            using (var xmlStream = rssSerializer.SerializeAsStream(rssSample))
            {
                //write response
                httpContext.Response.ContentType = ContentTypeOut;
                httpContext.Response.StatusCode = enumHttpStatusCode.OK;
                httpContext.Response.WriteStream(xmlStream);
            }

            httpContext.Response.Close();
            Debug.Print(GetType() + " executed, " + Debug.GC(false) + " bytes free");

            #region Debug-Output
            /*
                mfRCF.NetduinoPlus2.TestApp.rcfSampleRssGetCommand executed, 77628 bytes free
                mfRCF.NetduinoPlus2.TestApp.rcfSampleRssGetCommand executed, 75372 bytes free
                mfRCF.NetduinoPlus2.TestApp.rcfSampleRssGetCommand executed, 75420 bytes free
                mfRCF.NetduinoPlus2.TestApp.rcfSampleRssGetCommand executed, 77160 bytes free
                mfRCF.NetduinoPlus2.TestApp.rcfSampleRssGetCommand executed, 77160 bytes free
                mfRCF.NetduinoPlus2.TestApp.rcfSampleRssGetCommand executed, 77160 bytes free
                mfRCF.NetduinoPlus2.TestApp.rcfSampleRssGetCommand executed, 77160 bytes free
                mfRCF.NetduinoPlus2.TestApp.rcfSampleRssGetCommand executed, 77160 bytes free
                mfRCF.NetduinoPlus2.TestApp.rcfSampleRssGetCommand executed, 77160 bytes free
             */
            
            #endregion
        }
    }
}
